package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;

/**
 * description: 化验单, 增加
 * created by kalu on 2018/4/16 13:15
 */
public class AssayItemDialog extends BaseDialog implements View.OnClickListener {

    private final String tempStr = String.valueOf(-1);

    private String editName = tempStr; // 化验项
    private String editResult = tempStr; // 结果
    private String editValue = tempStr; // 参考值
    private String editUnit = tempStr; // 单位
    /************************************************/

    private OnDialogChangeListener listener;

    public AssayItemDialog(Activity activity) {
        super(activity);
    }

    @Override
    public void show() {
        super.show();
//        setCanceledOnTouchOutside(false);
//        setCancelable(false);
    }

    @Override
    public void dismiss() {

        if (null != listener && !tempStr.equals(editResult) && !tempStr.equals(editName) && !tempStr.equals(editValue) && !tempStr.equals(editUnit)) {
            listener.onEditChange(editName, editResult, editValue, editUnit);
        }

        super.dismiss();
    }

    @Override
    public void cancel() {

        if (null != listener && !tempStr.equals(editResult) && !tempStr.equals(editName) && !tempStr.equals(editValue) && !tempStr.equals(editUnit)) {
            listener.onEditChange(editName, editResult, editValue, editUnit);
        }

        super.cancel();
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_assay_item;
    }

    @Override
    protected void initData() {

        findViewById(R.id.layout_dialog_assay_back).setOnClickListener(this);
        findViewById(R.id.layout_dialog_assay_ok).setOnClickListener(this);

        final EditText edit1 = findViewById(R.id.layout_dialog_assay_a);
        if (null != edit1) {
            edit1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    final String trim = edit1.getText().toString().trim();
                    editName = trim;
                }
            });
        }

        final EditText edit2 = findViewById(R.id.layout_dialog_assay_b);
        if (null != edit2) {
            edit2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    final String trim = edit2.getText().toString().trim();
                    editResult = trim;
                }
            });
        }

        final EditText edit3 = findViewById(R.id.layout_dialog_assay_c);
        if (null != edit3) {
            edit3.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    final String trim = edit3.getText().toString().trim();
                    editValue = trim;
                }
            });
        }

        final EditText edit4 = findViewById(R.id.layout_dialog_assay_d);
        if (null != edit4) {
            edit4.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    final String trim = edit4.getText().toString().trim();
                    editUnit = trim;
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_dialog_assay_back:
                editName = tempStr;
                editResult = tempStr;
                editValue = tempStr;
                editUnit = tempStr;
                dismiss();
                break;
            case R.id.layout_dialog_assay_ok:
                dismiss();
                break;
        }
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {
        void onEditChange(String name, String result, String value, String unit);
    }
}
